package data;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import network.Connexion;


public class ModeleListeIndices extends AbstractTableModel implements Observer, TableModelListener{
	Connexion con;
	private Object[][] donnees;
	private final String[] entetes = {"Nom","Type"};
	private ArrayList<Indice> listIndices = new ArrayList<Indice>();
	ListeIndices listindices;
	
	public ModeleListeIndices()
	{
		super();
		this.addTableModelListener(this);
	}
	
	public int getRowCount() {
        return listIndices.size();
    }
	public ArrayList<Indice> getListIndice() {
		return listIndices;
	}

    public int getColumnCount() {
        return entetes.length;
    }

    public String getColumnName(int columnIndex) {
        return entetes[columnIndex];
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex){
            case 0:
                return listIndices.get(rowIndex).getLabelIndice();
            case 1:
                return listIndices.get(rowIndex).getTypeIndice();
            default:
                return null; 
        }
    }
      
    public void removeRow(int row) {
    	//listeindices.remove(row);
		fireTableStructureChanged();
	}
    
    //public void insertRow(int row, Indice a) {
    //	listIndices.add(row, a);
    // fireTableStructureChanged();
	//} 
    
    public void addRow(Indice a) {
		addRow(a);
		fireTableStructureChanged();
	}

	public void addIndice(Indice indice) {
		listIndices.add(indice);
		
        fireTableRowsInserted(listIndices.size() -1, listIndices.size() -1);
        
        //fireTableRowsInserted(  );
    }
	
	public void clear(){
		listIndices.clear();
		fireTableStructureChanged();
	}
	@Override
	public void update(Observable arg0, Object arg1){
		listindices = (ListeIndices) arg0;
		
		clear();
		
		System.out.println("UPDATE Indice");
		for(int count = 0; count <  listindices.size() ;count++ ){
			/*
			Indice a = new Indice();
            a.setIdIndice(0);
            a.setTypeIndice(listIndices.get(count).getLabelIndice());
            a.setLabelIndice(listIndices.get(count).getLabelIndice());
            a.setCheminIndice(listIndices.get(count).getCheminIndice());
            a.setTempIndice(listIndices.get(count).getTempIndice());
		    a.setStatu(listIndices.get(count).isStatu());
		    */
		    //System.out.println("update" + listIndices.get(count).getLabelIndice());
			Indice i = listindices.get(count);
			if(i.isPossedeIndice())
				addIndice(i);
		}
	}
	
	/*public ArrayList<Indice> getlistIndices() {
		return listIndices;
	}
	public void setlistIndices(ArrayList<Indice> listIndices) {
		this.listIndices = listIndices;
	}*/
	
	/*public void setValueAt(Object value, int row, int col) 
	{	
		try {
			updateIndices(value,row,col);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
	public void updateIndices(final Object value,final int row,final int col) throws SQLException
	{
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					int id = getlistIndices().get(row).getIdIndice();
					Indice a = DB.selectIndiceId(id);
					System.out.println("Nom colonne " + getColumnName(col));
					if(getColumnName(col).equals("Type")) 
						DB.updateLigneIndicesType(id,value.toString());
					else if(getColumnName(col).equals("Label"))
						DB.updateLigneIndicesLabel(id,value.toString());
					else if(getColumnName(col).equals("Chemin"))
						DB.updateLigneIndicesChemin(id,value.toString());
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}).start();
		
	}*/
	
	@Override
	public void tableChanged(TableModelEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
